package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.entity.Explain;
import com.wind.service.IExplainService;
import com.wind.vo.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/explain")
public class ExplainController extends BaseController {

    @Autowired
    private IExplainService explainService;

    @RequestMapping("/page")
    public Json page(@RequestBody String body) {
        log.info("explain page:{}", body);
        JSONObject json = JSON.parseObject(body);
        String name = json.getString("name");
        int current = json.getIntValue("current");
        int size = json.getIntValue("size");
        if (current == 0) current = 1;
        if (size == 0) size = 10;
        Page<Explain> page = explainService.selectPage(new Page<>(current, size),
                new EntityWrapper<Explain>()
                        .like("result_name", name));
        return Json.succ("page",page);
    }

    @RequestMapping("/add")
    public Json add(@RequestBody String body) {
        log.info("explain add:{}", body);
        Explain explain = JSON.parseObject(body, Explain.class);
        if (!explainService.insert(explain)) {
            return Json.fail("新增失败");
        }
        return Json.succ();
    }

    @RequestMapping("/update")
    public Json update(@RequestBody String body) {
        log.info("explain update:{}", body);
        Explain explain = JSON.parseObject(body, Explain.class);
        if (!explainService.updateById(explain)) {
            return Json.fail("操作失败");
        }
        return Json.succ();
    }

    @RequestMapping("/del")
    public Json del(@RequestBody String body) {
        log.info("explain del:{}", body);
        JSONObject json = JSON.parseObject(body);
        Integer id = json.getIntValue("id");
        Explain explain = explainService.selectById(id);
        if (explain == null) {
            return Json.fail("获取报告信息失败!");
        }
        explain.setIsActive(0);
        if (!explainService.updateById(explain)) {
            return Json.fail("删除失败!");
        }
        return Json.succ();
    }

}
